/**
 * Functionality specific to Bootstrapper.
 *
 * Provides helper functions to enhance the theme experience.
 */

( function( $ ) {


	//  Add class="btn" to buttons & inputs in the sidebar
		function sidebarButtons() {
			$('#sidebar-top button').addClass('btn');
			$('#sidebar-top input[type="submit"]').addClass('btn');
			$('#sidebar-bottom button').addClass('btn');
			$('#sidebar-bottom input[type="submit"]').addClass('btn');
		}
		sidebarButtons();  // Run on initial page load

	//  Affix the Sidebar with responsive settings
		function affixSidebar() {
			if ( 767 < $(window).width() ) {  // Make sure window is wide enough to affix the sidebar.
				var topSidebarHeight = $('#sidebar-top').outerHeight();  // Get top Sidebar height
				var bottomSidebarHeight = $('#sidebar-bottom').outerHeight();  // Get bottom Sidebar height
				var cumSidbarHeights = topSidebarHeight + bottomSidebarHeight;  // Calc cumulative height
				var siteContentHeight =  $('#site-content').height();  // Get the main content height
				var topSidebar_EnableOffsetBottom = bottomSidebarHeight + 80; // Enable bottom offset for the top-Sidebar
				$('#sidebar-bottom .inner').css( 'bottom', 60 )  // Bottom-Sidebar bottom-offset
				$('#sidebar-top .inner').css( 'bottom', bottomSidebarHeight + 60 )  // Top-Sidebar    bottom-offset
				if ( $(window).width() < 980 ) {   // if the Navbar is not fixed,
					if ( cumSidbarHeights < siteContentHeight ) {  // Don't affix if we don't need to.
						$('#sidebar-bottom .inner').css( 'top', topSidebarHeight + 20)	 // Bottom-Sidebar top-offset
						$('#sidebar-top .inner').affix({ offset:{ top:59, bottom: topSidebar_EnableOffsetBottom } });  // enable Top Sidebar
						$('#sidebar-bottom .inner').affix({ offset:{ top:59, bottom:80 } });  // enable Bottom Sidebar
					}
				} else {  // Else,
					if ( cumSidbarHeights < siteContentHeight ) {  // Don't affix if we don't need to.
						$('#sidebar-bottom .inner').css( 'top', topSidebarHeight + 60)	 // Bottom-Sidebar top-offset
						$('#sidebar-top .inner').affix({ offset:{ top:0, bottom: topSidebar_EnableOffsetBottom } });  // enable Top Sidebar
						$('#sidebar-bottom .inner').affix({ offset:{ top:0, bottom: 80 } });  // enable Bottom Sidebar
					}
				}
			}
		}
		affixSidebar();  // Run on initial page load

	//  Fix long page length when fixed Navbar is expanded 
		$('#site-header .nav-collapse').on('show', function () {
			$('#site-main').css('min-height', 'auto');  // Disable stick footer
		})
		$('#site-header .nav-collapse').on('shown', function () {
			var $mainNavHeight = $('#site-header').height();  // Get Navbar height
			$('#site-main').css('margin', '-'+$mainNavHeight+'px auto -60px auto');  // Mod stick footer CSS
			$('#push-site-header').css('height', $mainNavHeight);  // Mod stick footer CSS
			$('#site-main').css('min-height', '');  // Re-enable stick footer
		})
		$('#site-header .nav-collapse').on('hide', function () {
			$('#site-main').css('min-height', 'auto');  // Disable sticky footer
			$('#push-site-header').css('height', '');  // Remove CSS mod
			$('#site-main').css('margin', '');  // Remove CSS mod
		})
		$('#site-header .nav-collapse').on('hidden', function () {
			$('#site-main').css('min-height', '');  // Re-enable stick footer
		})
		
} )( jQuery );